<?php
class ImportWkXML {
    public function load($file) {
        $dom = new DOMDocument();
        $dom -> substituteEntities = true;
        $dom -> load($file);
        //$xpath = new DOMXpath($dom);

        $listDiv1 = $dom -> getElementsByTagName('div1');
        if ($listDiv1 -> length < 1) {
            var_dump('kein Element div1 gefunden.');
            return;
        }

        //get section_id;
        $section_id = '';
        $div1 = $listDiv1 -> item(0);
        $type = $div1 -> getAttribute('type');
        if ($type == 'section') {
            $section_id = $div1 -> getAttribute('xml:id');
        }

        //weil bestimmte Informationen aus xml in DB gespeichert werden müssen,es wäre besser, xml nur einmal parsen, nicht zweimal mit xsl
        /* $xsl = new DOMDocument();
         $xsl -> load('D:/Projekte/Hhp/briefe/wk.xsl');
         $proc = new XSLTProcessor;
         $proc -> importStyleSheet($xsl);
         $dom = $proc -> transformToDoc($dom);
         */

        $comment = D('comment');
        //Jede Element div2 entspricht einen Brief
        $listDiv2 = $div1 -> getElementsByTagName('div2');
        foreach ($listDiv2 as $elem) {
            $newDom = new DOMDocument();
            //$newDom->preserveWhiteSpace=false;
            //$newDom->formatOutput = true;
            $newRoot = $newDom -> createElement('temp');
            $newDom -> appendChild($newRoot);
            $data = array();
            $data['section_id'] = $section_id;
            $data = $this -> readNode($elem, $data, $newRoot, true);
            $data['html'] = $newDom -> saveHTML();
            // echo($newDom -> saveHTML());
            $comment -> add($data);
        }
    }

    private function readNode(DOMElement $elem, $data, $newRoot, boolean $skip) {
        if ($elem -> nodeType == XML_TEXT_NODE) {
            $text = $elem -> textContent;
            $text = preg_replace("[\n]", "", $text);
            //var_dump($text);
            $newRoot -> appendChild(new DOMText($text));
            return $data;
        }
        $tagName = $elem -> tagName;
        if (!$skip)
            switch($tagName) {
                case 'head' :
                    $type = $elem -> getAttribute('type');
                    if ($type == 'briefnr') {
                        $data['briefnr'] = preg_replace("[\D]", "", $elem -> textContent);
                    }
                    return $data;
                case 'lb' :
                    return $data;
                case 'byline' :
                    return $data;

                case 'div3' :
                    $div = $newRoot -> ownerDocument -> createElement('div');
                    $div -> setAttribute('class', $elem -> getAttribute('type'));
                    $newRoot -> appendChild($div);
                    break;
                case 'row' :
                    $tr = $newRoot -> ownerDocument -> createElement('tr');
                    $newRoot -> appendChild($tr);
                    break;
                case 'cell' :
                    $td = $newRoot -> ownerDocument -> createElement('td');
                    $roleAttr = $elem -> getAttribute('role');
                    $td -> setAttribute('class', $roleAttr);
                    $newRoot -> appendChild($td);
                    break;

                default :
                    $clone = $newRoot -> ownerDocument -> importNode($elem, FALSE);
                    $newRoot -> appendChild($clone);
                    $newRoot = $clone;
            }

        $cList = $elem -> childNodes;
        foreach ($cList as $c) {
            $data = $this -> readNode($c, $data, $newRoot);
        }
        return $data;
    }

    private function changeName($node, $name) {
        $newnode = $node -> ownerDocument -> createElement($name);
        foreach ($node->childNodes as $child) {
            $child = $node -> ownerDocument -> importNode($child, true);
            $newnode -> appendChild($child, true);
        }
        foreach ($node->attributes as $attrName => $attrNode) {
            $newnode -> setAttribute($attrName, $attrNode);
        }
        $newnode -> ownerDocument -> replaceChild($newnode, $node);
        return $newnode;
    }

}
